<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.w3.org/1999/xhtml">
<head>
    <meta charset="UTF-8">
    <title></title>
    <link rel="stylesheet" th:href="@{/bootstrap-3.3.7-dist/css/bootstrap.css}">
    <script th:src="@{/bootstrap-3.3.7-dist/jquery-1.9.1.js}"></script>
    <script th:src="@{/bootstrap-3.3.7-dist/js/bootstrap.js}"></script>
    <script type="text/javascript">
        $(function () {
            //查询
            queryAll();
            $.getJSON("/querySort", function (sortList) {
                $.each(sortList, function (index, p) {
                    var option = "<option value='" + p.id + "'>" + p.name + "</option>";
                    $("select").append(option);
                })
            })


            $("#save_info").click(function () {
                location.href = "/handler/toSave";
            })

            $("#cha_info").click(function () {
                var sortId = $("select").val();
                var data = 'sortId=' + sortId;
                queryAll(data);
            })


        })


        //列表
        function queryAll(data) {
            $("#word_info_table").html("");
            //获取表头
            $("#word_info_table").append("<tr><td>帖子ID</td><td>标题</td><td>作者</td><td>发布时间</td><td>回复次数</td></tr>")
            //获取数据
            $.getJSON("/queryAll", data, function (result) {
                $.each(result, function (index, d) {
                    var tr = "<tr><td>" + d.id + "</td>" +
                        "<td>" + d.title + "</td>" +
                        "<td>" + d.author + "</td>" +
                        "<td>" + new Date(d.createDate).toLocaleDateString() + "</td>" +
                        "<td>" + d.replyCount + "</td></tr>";
                    $("#word_info_table").append(tr);

                    var size = result.length;
                    if (index+1 == size) {
                        var type = $("input[type='hidden']").val();
                        if (type != '') {
                            if (type == 'success') {
                                var tr = "<tr><td colspan='5' class='text-center'>保存成功</td></tr>";
                                $("#word_info_table").append(tr);
                            }
                            if (type == 'error') {
                                var tr = "<tr><td colspan='5' class='text-center'>保存失败</td></tr>";
                                $("#word_info_table").append(tr);
                            }
                        }
                    }
                })
            })


        }
    </script>
</head>
<body>
<input type="hidden" th:value="${type}">
<div class="container" align="center">
    <h1>项目查询页面</h1>
    <br/>
    讨论版区：
    <select name="sortId">
        <option value="-1">全部</option>
    </select>
    <button type="button" id="cha_info" class="btn btn-success">查询</button>
    <div class="row text-right">
        <div class="col-md-10 col-md-offset-1">
            <button type="button" id="save_info" class="btn btn-default">发帖</button>
        </div>
    </div>
    <br/>
    <br/>
    <!--Table表格-->
    <div class="row">
        <div class="col-md-10 col-md-offset-1">
            <table class="table table-hover table-bordered" id="word_info_table"></table>
        </div>
    </div>


</div>
</body>
</html>